package com.new_wear.project.system.nav.controller;

import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.framework.web.domain.Ztree;
import com.new_wear.framework.web.page.TableDataInfo;
import com.new_wear.project.system.menu.service.MenuServiceImpl;
import com.new_wear.project.system.nav.domain.CstNav;
import com.new_wear.project.system.nav.service.ICstNavService;
import com.new_wear.project.system.role.domain.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZhangRenpeng
 * @Description:
 * @date 2020/1/8 9:36
 */
@Controller
@RequestMapping("/system/mobileNav")
public class MobileNavController extends BaseController {

    @Autowired
    private ICstNavService cstNavService;
    @Autowired
    private MenuServiceImpl menuService;

    private String prefix = "system/mobileNav";

    @RequiresPermissions("system:mobileNav:view")
    @GetMapping()
    public String nav()
    {
        return prefix + "/mobileNav";
    }

    /**
     * 查询系统 - 导航列表
     */
    @RequiresPermissions("system:mobileNav:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CstNav> list(CstNav cstNav)
    {
        startPage();
        cstNav.setTpe(2);
        List<CstNav> list = cstNavService.selectCstNavList(cstNav);
        return list;
    }

    /**
     * 导出系统 - 导航列表
     */
    @RequiresPermissions("system:mobileNav:export")
    @Log(title = "系统 - 导航", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstNav cstNav)
    {
        List<CstNav> list = cstNavService.selectCstNavList(cstNav);
        ExcelUtil<CstNav> util = new ExcelUtil<CstNav>(CstNav.class);
        return util.exportExcel(list, "nav");
    }

    /**
     * 新增系统 - 导航
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统 - 导航
     */
    @RequiresPermissions("system:mobileNav:add")
    @Log(title = "系统 - 导航", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstNav cstNav)
    {
        cstNav.setTpe(2);
        return toAjax(cstNavService.insertCstNav(cstNav));
    }

    /**
     * 修改系统 - 导航
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstNav cstNav = cstNavService.selectCstNavById(id);
        mmap.put("cstNav", cstNav);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统 - 导航
     */
    @RequiresPermissions("system:mobileNav:edit")
    @Log(title = "系统 - 导航", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstNav cstNav)
    {
        return toAjax(cstNavService.updateCstNav(cstNav));
    }

    /**
     * 删除系统 - 导航
     */
    @RequiresPermissions("system:mobileNav:remove")
    @Log(title = "系统 - 导航", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(cstNavService.deleteCstNavByIds(id.toString()));
    }


    /*树结构*/
    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Ztree> roleMenuTreeData(Role role)
    {
        List<Ztree> ztrees = menuService.roleMenuTreeData(role);
        return ztrees;
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Ztree> menuTreeData(Role role)
    {
        List<Ztree> ztrees = cstNavService.menuTreeData();
        return ztrees;
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{id}")
    public String selectMenuTree(@PathVariable("id") Integer id, ModelMap mmap)
    {

        mmap.put("group", cstNavService.selectCstNavById(id));
        return prefix + "/tree";
    }


    @GetMapping("/navAddType/{id}")
    public String areaAdd(@PathVariable("id") Integer id,ModelMap map){
        map.put("id",id);
        return prefix + "/navAddType";
    }
    @RequestMapping("/mobileNavAddType")
    @ResponseBody
    public AjaxResult addSublevel(@RequestParam("pid") Integer pid , CstNav cstNav){
        cstNav.setPid(pid);
        cstNav.setTpe(2);
        return toAjax(cstNavService.insertCstNav(cstNav));
    }
}
